package com.baozhun.mall.common.network

import android.os.Build
import com.baozhun.mall.common.util.CacheUtil
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
 * 公共参数拦截器
 */
class CommonParamsInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        if ("POST" == request.method()) {
            if (request.body() is FormBody) {
                val bodyBuilder = FormBody.Builder()
                var formBody = request.body() as FormBody
                for (i in 0 until formBody.size()) {
                    bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i))
                }
                formBody = bodyBuilder
//                    .addEncoded("uid", "1")
//                    .addEncoded("source", "app")
//                    .addEncoded("platform", "android")
//                    .addEncoded("platform_version", Build.VERSION.RELEASE)
//                    .addEncoded("device_id", "abc")
//                    .addEncoded("version_code", "1000000")
                    .build()
                request = request.newBuilder()
//                    .addHeader("Authorization", "xxx")
                    .post(formBody).build();
            }
        } else if ("GET" == request.method()) {
            val httpUrl = request.url().newBuilder()
//                .addQueryParameter("uid", "1")
//                .addQueryParameter("source", "app")
//                .addQueryParameter("platform", "android")
//                .addQueryParameter("platform_version", Build.VERSION.RELEASE)
//                .addQueryParameter("device_id", "abc")
//                .addQueryParameter("version_code", "1000000")
                .build()
            request = request.newBuilder()
//                .addHeader("Authorization", "xxx")
                .url(httpUrl).build()
        }
        return chain.proceed(request)
    }
}